<template>
  <view class="search-box">
    <!-- 搜索框 -->
    <uni-search-bar :radius="25" bgColor="#ffaa7f" cancelButton="none" @input="input"></uni-search-bar>
    <!-- 历史记录 -->
    <view class="history-box" v-if="kw === ''">
      <!-- 历史标题 -->
      <view class="history-title">
        <view class="history-name">
          历史搜索纪录
        </view>
        <uni-icons @click="clear" type="trash-filled" size="30"></uni-icons>
      </view>
      <!-- 历史记录列表 -->
      <view class="history-list">

        <view @click="goToGoodslist(item)" class="history-item" v-for="(item,index) in history" :key="index">
          <uni-tag :text="item" type="primary" :circle="true"></uni-tag>
        </view>
      </view>
    </view>
    <!-- 搜索列表 -->
    <view class="seachResult-list" v-else>
      <view @click="goToGoodsdetail(item)" class="seachResult-item" v-for="(item,index) in searchRsult" :key="index">
        <view class="item-name">
          {{item.goods_name}}
        </view>
        <uni-icons type="right" size="25"></uni-icons>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null,
        kw: '',
        searchRsult: [],
        history: []
      };
    },
    computed: {
      historys: function(){
        return [...this.history.reverse()]
      }
    },
    onLoad() {
      this.history = JSON.parse(uni.getStorageSync() || '[]')
    },
    methods: {
      clear(){
        this.history = [],
        uni.setStorageSync('kw','[]')
      },
      getHistory(){
        const set = new Set(this.history)
        set.delete(this.kw)
        set.add(this.kw)
        this.history = Array.from(set)
        uni.setStorageSync(
        'kw',JSON.stringify(this.history))
      },
      goToGoodsdetail(item) {
        // console.log(item.goods_id)
        uni.navigateTo({
          url: '../goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }, 
       goToGoodslist(kw) {
        // console.log(item.goods_id)
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + kw
        })
      },
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e
          // console.log(this.kw)
          this.getSearchRsult();
        }, 500);
      },
      async getSearchRsult() {
        if (this.kw === '') {
          this.searchRsult = []
          return
        }
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.kw});
        console.log(res);
        if (res.meta.status === 200) {
          this.searchRsult = res.message
          this.getHistory();
        }
      }
    }
  }
</script>

<style lang="scss">
  .uni-searchbar {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    position: relative;
    padding: 16rpx;
    /* 将默认的 #FFFFFF 改为 #C00000 */
    background-color: #c00000;
    text-align: center;
  }

  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;

    .seachResult-list {
      padding: 0, 10px;

      .seachResult-item {
        display: flex;
        font-size: 12px;
        padding: 13px 0;
        border-bottom: 2px solid #000000;
        align-items: center;
        justify-content: space-between;

        .item-name {
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          margin-right: 5px;
        }
      }
    }
  }

  .history-box {
    .history-title {
      display: flex;
      align-items: center;
      justify-content: space-between;
      // justify-content: space-around;
      font-size: 13px;
      height: 40px;
      border-bottom: 2px solid #000000;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      .history-item {
        margin: 5px 10px;
      }
    }
  }
</style>
